package com.github.base.list

import android.annotation.SuppressLint
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding


/**
 * RecyclerView 列表基类1
 * @author Mr.xie
 * @Date  2025/4/24
 */

abstract class BaseRecyclerViewAdapter1<VB : ViewBinding, D> :
    RecyclerView.Adapter<BaseRecyclerViewAdapter1ViewHolder<VB>>() {

    var itemBinding: VB? = null

    var listData = mutableListOf<D>()

    var isOverRideViewType = false;

    open abstract fun onBindViewHolder(
        holder: BaseRecyclerViewAdapter1ViewHolder<VB>,
        position: Int,
        data: D,
        binding: VB
    )


    open abstract fun setItemBinding(parent: ViewGroup, viewType: Int) : VB
    override fun onCreateViewHolder(
        parent: ViewGroup, viewType: Int
    ): BaseRecyclerViewAdapter1ViewHolder<VB> {

        itemBinding = setItemBinding(parent,viewType);
        return BaseRecyclerViewAdapter1ViewHolder(itemBinding!!);
    }

    override fun onBindViewHolder(
        holder: BaseRecyclerViewAdapter1ViewHolder<VB>, position: Int
    ) {
        onBindViewHolder(holder, position, listData[position], holder.itemBinding)
    }

    override fun getItemCount(): Int {
        return this.listData.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (isOverRideViewType) return position else return super.getItemViewType(position);
    }

}


/**
 * 设置数据
 */
@SuppressLint("NotifyDataSetChanged")
fun <VB : ViewBinding, D> BaseRecyclerViewAdapter1<VB, D>.setData(data: MutableList<D>) {
    this.listData = data;
    notifyDataSetChanged()
}


fun <VB : ViewBinding, D> BaseRecyclerViewAdapter1<VB, D>.setViewType(isSet: Boolean) {
    this.isOverRideViewType = isSet;
}


class BaseRecyclerViewAdapter1ViewHolder<VB : ViewBinding>(var itemBinding: VB) :
    RecyclerView.ViewHolder(itemBinding.root) {}



